package com.sandking.db.jedis;

import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import com.sandking.db.bean.Fwqtjbb;
import java.util.List;
import com.sandking.db.dao.FwqtjbbDao;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;

public class FwqtjbbJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Fwqtjbb getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Fwqtjbb fwqtjbb = null;
		try{
			String key = "Fwqtjbb_Object";
			String field = SK_Plus.b("id:",id).e();
			fwqtjbb = Fwqtjbb.createForJson(jedis.hget(key,field));
			if(fwqtjbb == null){
				fwqtjbb = FwqtjbbDao.getById(id);
				if(fwqtjbb!=null){
					loadCache(fwqtjbb);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return fwqtjbb;
	}
	

	public static void loadCache(Fwqtjbb fwqtjbb){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(fwqtjbb,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Fwqtjbb fwqtjbb,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Fwqtjbb_Object";
		String field = SK_Plus.b("id:",fwqtjbb.getId()).e();
		String data = fwqtjbb.toJson();
		p.hset(key,field,data);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Fwqtjbb> fwqtjbbs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Fwqtjbb fwqtjbb : fwqtjbbs){
				loadCache(fwqtjbb,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Fwqtjbb fwqtjbb){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(fwqtjbb,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Fwqtjbb fwqtjbb,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Fwqtjbb_Object";
		String field = SK_Plus.b("id:",fwqtjbb.getId()).e();
		p.hdel(key,field);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Fwqtjbb> fwqtjbbs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Fwqtjbb fwqtjbb : fwqtjbbs){
				clearCache(fwqtjbb,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Fwqtjbb insert(Fwqtjbb fwqtjbb){
		fwqtjbb = FwqtjbbDao.insert(fwqtjbb);
		if(fwqtjbb!=null){
			loadCache(fwqtjbb);
		}
    	return fwqtjbb;
    }
    
    public static Fwqtjbb update(Fwqtjbb fwqtjbb){
    	fwqtjbb = FwqtjbbDao.update(fwqtjbb);
    	if(fwqtjbb!=null){
			loadCache(fwqtjbb);
		}
    	return fwqtjbb;
    }
    
    public static boolean delete(Fwqtjbb fwqtjbb){
    	boolean bool = FwqtjbbDao.delete(fwqtjbb);
    	if(bool){
    		clearCache(fwqtjbb);
    	}
    	return bool;
    }
}